<?php

/**
 *
 * Controller for managing the account of a user.
 * @author Théo Chamley
 *
 */
class GestioncarteController extends CodeBar_AbstractController {

	public function init() {
		CodeBar_AbstractController::init();
		if(!($this->session()->user->connected)) {
			$this->_helper->redirector('index');
		} else if($this->session()->permissionactive != "GestionCarte") {
			$this->_helper->redirector('PermissionChoice','index');
		}
	}

	/**
	 * 
	 * Unused.
	 */
	public function indexAction() {
		$this->view->title = "Gestion de Carte";
		$this->view->headLink()->appendStylesheet('/css/GestionCarte.css');
		$this->view->headScript()->appendFile('/js/GestionCarte.js','text/javascript');
	}

	public function gestionAction() {
		$this->view->headTitle("Gestion de carte");
		/*
		 * CSS
		 */
		$this->view->headLink()->appendStylesheet('/css/GestionCarte.css');
		$this->view->headLink()->appendStylesheet('/css/histo_table.css');
		
		/*
		 * JS
		 */
		$this->view->headScript()->appendFile('/js/jquery.dataTables.min.js','text/javascript');
		$this->view->headScript()->appendFile('/js/TableTools.min.js','text/javascript');
		$this->view->headScript()->appendFile('/js/GestionCarte.js','text/javascript');
		$this->view->headScript()->appendFile('/js/jquery.dataTables.dateSorting.js','text/javascript');
	}

	public function nouveauclientAction() {
		$this->_helper->viewRenderer->setNoRender();
		$this->_helper->layout->disableLayout();
		$user = null;
		$carte = null;
		$succes = 0;
		if(isset($_POST['id_carte']) && ($_POST['id_carte'] != '')) {
			if(is_numeric($_POST['id_carte'])) {
				$carte = new Application_Model_Carte();
				$carte = $carte->getById($_POST['id_carte']);
				if($carte->getId() != 0) {
					$succes = 1;
					$this->session()->carteId = $carte->getId();
					$user = new Application_Model_User();
					$user = $user->getById_Carte($_POST['id_carte']);
				}
			} else {
				$user = Application_Model_User::getByLogin(strtolower($_POST['id_carte']));
				if($user->getLogin()!='echec') {
					$succes = 1;
					$carte = $user->getCarte();
					$this->session()->carteId = $carte->getId();
				}
			}
		} else if(isset($_POST['login'])) {
			if(!is_numeric($_POST['login'])) {//added 23/10/2011
				$user = Application_Model_User::getByLogin($_POST['login']);
				if($user->getLogin()!='echec') {
					$succes = 1;
					$carte = $user->getCarte();
					$this->session()->carteId = $carte->getId();
				}
			} else {//added 23/10/2011
				$carte = new Application_Model_Carte();
				$carte = $carte->getById($_POST['login']);
				if($carte->getId() != 0) {
					$succes = 1;
					$this->session()->carteId = $carte->getId();
					$user = new Application_Model_User();
					$user = $user->getById_Carte($_POST['login']);
				}
			}
		}
		if($succes==1) {
			if($carte->getId()!=0) {
				$bloquee=0;
			} else {
				$bloquee=1;
			}

			$infos= $user->getNom()."<br/>".$user->getPrenom()."<br/>".$user->getSolde().'€';
			echo json_encode(array(
				'infos' => $infos,
				'id_carte' =>$user->getIdCarte(),
				'bloquee'=>$bloquee,
				'succes'=>$succes
			));
		} else {
			echo json_encode(array(
				'succes'=> $succes
			));
		}
	}

	public function bloquerAction() {
		$this->_helper->viewRenderer->setNoRender();
		$this->_helper->layout->disableLayout();
		$carte = new Application_Model_Carte();
		$carte->getById($this->session()->carteId);
		if(isset($_POST['formname'])) {
			if($_POST['formname']=='block') {
				$carte->BloquerCarte();
				echo '<div class="block hover"id="debloquer"><h3> Débloquer la carte</h3></div>';
			}
		}
	}

	public function debloquerAction() {
		$this->_helper->viewRenderer->setNoRender();
		$this->_helper->layout->disableLayout();
		$carte = new Application_Model_Carte();
		$carte->getById($this->session()->carteId);
		if(isset($_POST['formname'])) {
			if($_POST['formname'] == 'unblock') {
				$carte->DebloquerCarte();
				echo '<div class="block" id="bloquer"><h3> Blocage de la carte</h3></div><br/><div class="block" id="especes"><h3> Paiement en espèces </h3></div><br/><div class="block" id="carte"><h3> Paiement par carte </h3></div><br/><div class="block" id="cheque"><h3> Paiement par chèque </h3></div><br/>';
			}
		}
	}

	public function rechargerAction() {
		//TODO récupérer le numéro de carte par POST et pas dans la $_SESSION
		//=> possible fix of issue #23
		$this->_helper->viewRenderer->setNoRender();
		$this->_helper->layout->disableLayout();
		
		$carte = new Application_Model_Carte();
		$carte->getById($this->getRequest()->getPost('carteId'));
		
		$user = new Application_Model_User();
		$user = $user->getById_Carte($carte->getId());
		$solde = $this->getRequest()->getPost('solde');
		
		if($solde > 0) {
			$moyen = $this->getRequest()->getPost('moyen');
			$renseignement = null;
			if($moyen != 'especes') {
				$renseignement = $this->getRequest()->getPost('renseignement');
			}
			$user->RechargerCarte($solde, $moyen, $renseignement);
		}
		echo $user->getPrenom().'<br/>'.$user->getNom().'<br/>'.$user->getSolde().'€';
	}

	public function histoAction() {
		$this->_helper->layout->disableLayout();
		$carte = new Application_Model_Carte();
		$carte->getById($this->session()->carteId);
		$user = new Application_Model_User();
		$user = $user->getById_Carte($carte->getId());
		$this->view->histo = Application_Model_HistoItem::getByClient($user);
	}
}
